
package Servlets;

import BeanManager.UserManager;
import Beans.User;
import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author r_uddie
 */

public class LoginServlet extends HttpServlet
{

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */

    protected void processRequest( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException
    {
        response.setContentType( "text/html;charset=UTF-8" );

        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(false);
       // UnsubscribeServlet unsubscriber; 

        // Get Username and Password from the HTML Form ..
        String username = request.getParameter( "username" );
        String password = request.getParameter( "password" );
        String unsubscribe = request.getParameter( "unsubscribe" );
        String changepassword = request.getParameter( "changepassword" );

        // If Nothing Is Returned ...
        if ( username == null || password == null )
        {
            out.println( "Please enter a username and password" );
        }
        // If Empty ...
        else if ( username.trim().isEmpty() || password.trim().isEmpty() )
        {
            out.println( "Please enter a username and password" );
        }
        else
        {
            // Get User Information From Database ...
            User user = UserManager.getUser( username ); 

            // No Such User Exists ?
            if ( user == null )
            {
                out.println( "Invalid Username" );
            }
            // Username and Password are Correct ?
            else if ( user.getPassword().equals( UserManager.encrypt( UserManager.encrypt( password ) ) ) )
            {
                if(unsubscribe != null)
                {
                    out.println( UserManager.deleteUser( username ) + "<form method=\"POST\" action=\"login.jsp\" ><input type=\"submit\" value=\"Proceed to Login\" /></form>" );
                    
                }
                else
                {
                session = request.getSession(true);
                session.setAttribute("user", user);
                
                out.println( "<h2>Congratulations " + user.getFirstName() + " !!</h2> <form method=\"POST\" action=\"logout.jsp\" ><input type=\"submit\" value=\"Proceed to Logout\" /></form>");
                }
            }
            else // Username Exists .. But Password Doesnt Match ..
            {
                out.println( "Wrong Password ..." );   
            }
        }

        // Free Resources ..
        out.close();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet( HttpServletRequest request , HttpServletResponse response )
            throws ServletException , IOException
    {
        processRequest( request , response );
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost( HttpServletRequest request , HttpServletResponse response )
            throws ServletException , IOException
    {
        processRequest( request , response );
    }

    // </editor-fold>
}
